#include "preloader.h"

static int objListCreate(void *obj)
{
	detect_obj_t *object = obj;

	object->llist = llist_init(object->type, object->size);

	if (NULL == object->llist)
	{
		return FAILED;
	}

	return SUCC;
}

static int objNumber(void *obj)
{
	detect_obj_t *object = obj;
	object->number = llist_number(object->llist);
	return SUCC;
}

static int objAdd(void *obj)
{
	detect_obj_t *object = obj;

	if (llist_insert(object->llist, object->obj, object->cmp, object->type) < 0)
	{   
		return FAILED;
	}   

	return SUCC;
}

static int objMod(void *obj)
{
	detect_obj_t *object = obj;

	if (llist_modify_node(object->llist, object->obj, object->cmp, object->entry) < 0)
	{
		return FAILED;
	}

	return SUCC;
}

static int objDel(void *obj)
{
	detect_obj_t *object = obj;

	if (llist_delete(object->llist, object->obj, object->cmp) < 0)
	{   
		return FAILED;
	}   

	return SUCC;
}

static int objFind(void *obj)
{
	int ret;
	detect_obj_t *object = obj;

	switch (object->type)
	{
		case ADDR:
			object->entry = llist_find__(object->llist, object->obj, object->cmp);
			return (NULL == object->entry) ? FAILED : SUCC;
		case DATA:
			ret = llist_find(object->llist, object->obj, object->cmp, object->entry);
			return (ret < 0) ? FAILED : SUCC;
		default:
			return FAILED;
	}

	return SUCC;
}

static int objTravel(void *obj)
{
	detect_obj_t *object = obj;

	llist_travel(object->llist, object->travel);

	return SUCC;
}

static int objIsEmpty(void *obj)
{
	detect_obj_t *object = obj;

	return (llist_isempty(object->llist) ? TRUE : FALSE);
}

int detectObjCall(const int type, void *obj)
{
	switch (type)
	{
		case OBJ_ADD:
			return objAdd(obj);
		case OBJ_DEL:
			return objDel(obj);
		case OBJ_FIND:
			return objFind(obj);
		case OBJ_MOD:
			return objMod(obj);
		case OBJ_TRAVEL:
			return objTravel(obj);
		case OBJ_NUMBER:
			return objNumber(obj);
		case OBJ_ISEMPTY:
			return objIsEmpty(obj);
		case OBJ_INIT:
			return objListCreate(obj);
		default:
			return FAILED;
	}

	return SUCC;
}

